package com.example.mqtt.conntroller;

import com.example.mqtt.common.enums.QueueStatus;
import com.example.mqtt.entity.QueueInfo;
import com.example.mqtt.model.Card;
import com.example.mqtt.model.ConsumeMessage;
import com.example.mqtt.service.QueueService;
import com.example.mqtt.service.WebSocketService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author qwert
 * @version 1.0.0
 * @create 2025/5/3 15:51
 **/
@RestController
@RequiredArgsConstructor
public class WebSocketController {

    private final WebSocketService webSocketService;
    private final QueueService queueService;

    @PostMapping("/add-card")
    public String addCard(@RequestBody Card card) {
        webSocketService.sendAddCard(card);

        QueueInfo queueInfo = QueueInfo.builder()
                .cardId(UUID.randomUUID().toString().substring(0, 6))
                .cardName("manbo" + System.currentTimeMillis())
                .username(card.getName())
                .phone(card.getContact())
                .build();
        queueService.pushQueue(queueInfo);
        return "Card added";
    }

    @PostMapping("/remove-top-card")
    public String removeTopCard() {
        webSocketService.sendRemoveTopCard();
        return "Top card removed";
    }

    @PostMapping("/consume-queue")
    private String consumeQueue(@RequestBody ConsumeMessage consumeMessage) {
        QueueStatus queueStatus = QueueStatus.fromCode(consumeMessage.getQueueStatus());
        webSocketService.consumeQueue(consumeMessage.getSequenceId(), queueStatus);
        return "consume queue";
    }
}
